﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication
{
    class FastMovieSearchString: IEquatable<FastMovieSearchString>
    {
        private string originalTitle;
        public string OriginalTitle
        {
            get { return originalTitle; }
        }
        private List<string> year;
        private static Regex isYear;
        private static Regex alphaOnly;
        private static Regex isResolution;
        private List<string> subTitlePieces;

        static FastMovieSearchString()
        {
            isYear = new Regex(@"^(19|20)\d\d$");
            alphaOnly = new Regex(@"^[a-zA-Z\-\']+$");
            isResolution = new Regex(@"^((720|1080)[pP]|[xX]264|[hH][Dd][Tt][Vv])$");
        }

        public FastMovieSearchString(string title)
        {
            originalTitle = title;
            year = new List<string>();
            subTitlePieces = new List<string>();

            List<string> pieces = new List<string>(
                title.Split(
                    new char[] { '.', '_', '-', '\\', '/', '(', ')', '!', '@', '#', ' ', '\t', '\'', '"', '{', '}', '$', '&', '%', '*', '^'}, 
                    StringSplitOptions.RemoveEmptyEntries));

            foreach (string piece in pieces)
            {
                if (isYear.IsMatch(piece))
                    year.Add(piece);
                else if (isResolution.IsMatch(piece))
                    continue;
                else if (alphaOnly.IsMatch(piece))
                    subTitlePieces.Add(piece);
                else
                    continue;
            }
        }

        #region IEquatable<FastMovieSearchString> Members

        public bool Equals(FastMovieSearchString other)
        {
            bool yearMatch = false;
            if (this.year.Count == 0 || other.year.Count == 0)
                yearMatch = true;
            else
            {
                foreach (string year in this.year)
                {
                    if (other.year.Contains(year))
                    {
                        yearMatch = true;
                        break;
                    }
                }
            }

            if (!yearMatch)
                return false;

            if (this.subTitlePieces.Count - other.subTitlePieces.Count != 0)
                return false;

            foreach (string piece in this.subTitlePieces)
            {
                if (!other.subTitlePieces.Contains(piece))
                    return false;
            }

            return true;
        }

        #endregion
    }
}
